package com.example.personalizededucationsystem.controller;

import com.example.personalizededucationsystem.dto.dto.ClassResponseDto;
import com.example.personalizededucationsystem.dto.request.CreateClassRequest;
import com.example.personalizededucationsystem.dto.request.StudentClassInfoDTO;
import com.example.personalizededucationsystem.dto.response.ClassResponse;
import com.example.personalizededucationsystem.dto.response.StudentMemberResponse;
import com.example.personalizededucationsystem.model.Class;
import com.example.personalizededucationsystem.model.User;
import com.example.personalizededucationsystem.service.ClassService;
import com.example.personalizededucationsystem.utils.IP_VALUE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin(origins = IP_VALUE.IP)
@RestController
@RequestMapping("/api/classes")
public class ClassController {

    private final ClassService classService;

    @Autowired
    public ClassController(ClassService classService) {
        this.classService = classService;
    }

    /**
     * 创建班级的API端点。
     * 只有拥有 'TEACHER' 角色的用户才能访问。
     */
    @PostMapping
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<ClassResponse> createClass(@RequestBody CreateClassRequest request, @AuthenticationPrincipal User currentUser) {

        Class createdClass = classService.createClass(request, currentUser);
        return new ResponseEntity<>(ClassResponseDto.fromEntity(createdClass), HttpStatus.CREATED);
    }

    /**
     * 获取当前教师创建的所有班级的API端点。
     */
    @GetMapping("/my-classes")
    @PreAuthorize("hasRole('TEACHER')")
    public ResponseEntity<List<ClassResponse>> getMyClasses(@AuthenticationPrincipal User currentUser) {
        List<Class> classes = classService.getClassesByTeacher(currentUser.getUserId());
        List<ClassResponse> response = classes.stream()
                .map(ClassResponseDto::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(response);
    }

    /**
     * 学生加入班级的端点
     */
    @PostMapping("/join/{invitationCode}")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<Void> joinClass(@PathVariable String invitationCode, @AuthenticationPrincipal User currentUser) {
        classService.joinClass(invitationCode, currentUser);
        return ResponseEntity.ok().build();
    }


    @GetMapping("/join/list")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<List<StudentClassInfoDTO>> checkClassList(@AuthenticationPrincipal User currentUser) {
        return ResponseEntity.ok(classService.getClasses(currentUser));
    }


    @GetMapping("/{classId}/students")
    @PreAuthorize("hasRole('TEACHER') or hasRole('STUDENT')")
    public ResponseEntity<List<StudentMemberResponse>> getStudentsInClass(@PathVariable Long classId, @AuthenticationPrincipal User currentUser) {
        List<StudentMemberResponse> students = classService.getStudentsInClass(classId, currentUser)
                .stream()
                .map(StudentMemberResponse::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(students);
    }

}
